import { BattleMap } from './BattleMap';
import { BuildingType, UnitType } from './Enums';
import { BuildingModel, UnitModel } from './Model';
import { BuildingConfig, UnitConfig } from './Const';

export class BattleFactory {
    private _map: BattleMap;

    private _nextUnitId: number = 1;
    public getUnitId(): number {
        return this._nextUnitId++;
    }

    private _nextBuildingId: number = 1;
    public getBuildingId(): number {
        return this._nextBuildingId++;
    }

    constructor(map: BattleMap) {
        this._map = map;
    }

    public createBuilding(player: string, type: BuildingType, position: { x: number; y: number }): BuildingModel {
        const buildingId = this.getBuildingId();
        const config = BuildingConfig[type];
        const building: BuildingModel = {
            id: buildingId,
            type,
            player,
            hp: config.hp,
            attack: config.attack,
            position,
        };

        // 添加建筑到玩家管理
        this._map.addBuilding(building);

        // 更新地图格子信息
        this._map.map[position.y][position.x].building = buildingId;
        this._map.map[position.y][position.x].player = player;

        return building;
    }

    public createUnit(player: string, type: UnitType, position: { x: number; y: number }): UnitModel {
        const unitId = this.getUnitId();
        const config = UnitConfig[type];
        const unit: UnitModel = {
            id: unitId,
            type,
            player,
            hp: config.hp,
            attack: config.attack,
            speed: config.speed,
            position,
        };

        // 添加单位到玩家管理
        this._map.addUnit(unit);

        // 更新地图格子信息
        this._map.map[position.y][position.x].unit = unitId;
        this._map.map[position.y][position.x].player = player;

        return unit;
    }
}
